package com.cpi.cornpp.logs.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.logs.dto.LogsDTO;
import com.cpi.cornpp.logs.exception.LogsException;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;
/**
 * 
 * @description: This Class is used to handle the Logs Data Access
 * @author Edgar Joao
 */
public class LogsDAO extends DBDAO{
	public static final String SQL_INSERT_LOG = "INSERT INTO logs (table_id, pk_id, user_id, description, timestamp) values (?,?,?,?,getdate())";//$NON-NLS-1$

	public void insertLog(LogsDTO logsDTO) throws LogsException{
		PreparedStatement preparedStatement = null;				
		try{										
			preparedStatement = prepareSQL(SQL_INSERT_LOG, dsName);	
			preparedStatement.setInt(1, logsDTO.getTableId());
			preparedStatement.setInt(2, logsDTO.getPkId());
			preparedStatement.setString(3, logsDTO.getUserId());
			preparedStatement.setString(4, logsDTO.getDescription());	
			executeInsert(preparedStatement);						
		}catch (PoolException poolException){
			LogsException logsException = null;
			poolException.printStackTrace(System.out);
			logsException = new LogsException(poolException, CornPPException.LAYER_DAO,CornPPException.ACTION_INSERT);
			throw logsException;	
		} catch (SQLException sqlException) {
			LogsException logsException = null;
			sqlException.printStackTrace(System.out);
			logsException = new LogsException(sqlException, CornPPException.LAYER_DAO, CornPPException.ACTION_INSERT);
			throw logsException;
		} finally {			
			try {				
				closeConnection(preparedStatement.getConnection());				
			} catch (SQLException sqle) {
				sqle.printStackTrace(System.out);
			}
		}
	}

}
